package view.settlement;

import java.awt.BorderLayout;
import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.event.DocumentEvent;
import javax.swing.DefaultComboBoxModel;
import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.JLabel;
import javax.swing.JOptionPane;

import java.awt.Font;
import javax.swing.JComboBox;
import javax.swing.LayoutStyle.ComponentPlacement;

import java.util.List;
import java.util.Map;
import java.util.Set;

import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;
import javax.swing.text.Document;

import entiey.Bill;
import entiey.OrderFood;
import entiey.OrderList;
import entiey.Table;
import entiey.Vip;
import entiey.Waiter;
import service.BillService;
import service.OrderFoodService;
import service.OrderListService;
import service.TableService;
import service.VipService;
import service.WaiterService;
import service.impl.BillServiceImpl;
import service.impl.OrderFoodServiceImpl;
import service.impl.OrderListServiceImpl;
import service.impl.TableServiceImpl;
import service.impl.VipServiceImpl;
import service.impl.WaiterServiceImpl;
import util.CastUtil;
import view.menu.DocumentAdapter;
import view.vip.V_vipMain;

import java.awt.Color;
import javax.swing.JButton;
import javax.swing.JTextField;
import javax.swing.SwingConstants;
import java.awt.Component;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.ActionEvent;
import java.awt.event.WindowEvent;

public class V_Pay extends JFrame {

	private JPanel contentPane;
	private JTable table;
	private JTextField text_search;
	private JTextField text_totalprice;
	private JTextField text_getmoney;
	private JTextField text_yuanprice;
	private JTextField text_discount;
	private JComboBox<String> cb_table;
	private OrderFoodService ofs=new OrderFoodServiceImpl();
	private List<OrderFood> orderlist;//订单表
	private JComboBox cb_waiter;
	private JTextField text_change;
	private JLabel lbl_num;
	private JLabel lbl_sum;
	private JLabel lbl_vip;
	private Vip vip;
	private VipService vipService=new VipServiceImpl();
	private OrderFoodService foodService=new OrderFoodServiceImpl();
	private JLabel lbl_orderNo;
	private JLabel lbl_integral;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					V_Pay frame = new V_Pay();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	public V_Pay() {
		setTitle("结算");
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 997, 611);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		
		JLabel lblNewLabel = new JLabel("收银员：");
		lblNewLabel.setFont(new Font("宋体", Font.PLAIN, 15));
		
		cb_waiter = new JComboBox();
		
		JLabel lblNewLabel_1 = new JLabel("订单号：");
		lblNewLabel_1.setFont(new Font("宋体", Font.PLAIN, 15));
		
		cb_table = new JComboBox();
		cb_table.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				//监听选项变化
				String selectedItem = (String) cb_table.getSelectedItem();
				//根据下拉框选项，展示订单
				orderlist = ofs.findByOrderNo(selectedItem);
				initTable(orderlist);
				
				double money = 0;
				int num=0;
				for (OrderFood orderFood : orderlist) {
					money+=orderFood.getFoodPrice()*orderFood.getFoodNum();
					num+=orderFood.getFoodNum();
				}
				//数量
				lbl_num.setText(num+"");
				//总计
				
				lbl_sum.setText(money+"");
			}
		});
		
		JScrollPane scrollPane = new JScrollPane();
		
		JLabel lblNewLabel_2 = new JLabel("数量:");
		lblNewLabel_2.setFont(new Font("宋体", Font.PLAIN, 15));
		
		lbl_num = new JLabel("0");
		lbl_num.setForeground(Color.RED);
		lbl_num.setFont(new Font("宋体", Font.PLAIN, 15));
		
		JLabel lblNewLabel_4 = new JLabel("总计：");
		
		lbl_sum = new JLabel("￥0");
		lbl_sum.setForeground(Color.RED);
		lbl_sum.setFont(new Font("宋体", Font.PLAIN, 15));
		
		JButton btnNewButton = new JButton("取单");
		btnNewButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				double money = 0;
				for (OrderFood orderFood : orderlist) {
					money+=orderFood.getFoodPrice()*orderFood.getFoodNum();
				}
				text_yuanprice.setText(money+"");//总价
			}
		});
		
		JLabel lblNewLabel_6 = new JLabel("会员：");
		lblNewLabel_6.setFont(new Font("宋体", Font.PLAIN, 15));
		
		text_search = new JTextField();
		text_search.setColumns(10);
		
		JButton btnNewButton_1 = new JButton(" 查询");
		btnNewButton_1.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				if("".equals(text_search.getText())) {
					V_vipMain vipMain=new V_vipMain();
					vipMain.setBounds(V_Pay.this.getX(), V_Pay.this.getY(), vipMain.getWidth(), vipMain.getHeight());
					vipMain.setVisible(true);
					vipMain.addWindowListener(new WindowAdapter() {
						public void windowClosed(WindowEvent e) {
							vip = vipMain.getVip();
							lbl_vip.setText(vip.getName());
							lbl_orderNo.setText(vip.getCard()+"");
							lbl_integral.setText(vip.getIntegral()+"");
							text_discount.setText(vip.getDiscount());
							text_totalprice.setText(CastUtil.castDouble(vip.getDiscount())*CastUtil.castDouble(text_yuanprice.getText())+"");
						};
					});
				}else {
					//根据会员手机号查询
					vip = vipService.findByTel(text_search.getText());
					System.out.println(vip.getName());
					lbl_vip.setText(vip.getName());
					lbl_orderNo.setText(vip.getCard()+"");
					lbl_integral.setText(vip.getIntegral()+"");
					text_discount.setText(vip.getDiscount());
					text_totalprice.setText(CastUtil.castDouble(vip.getDiscount())*CastUtil.castDouble(text_yuanprice.getText())+"");
				}
				
			}
		});
		
		JLabel lblNewLabel_7 = new JLabel("应收：");
		lblNewLabel_7.setFont(new Font("宋体", Font.BOLD, 30));
		
		text_totalprice = new JTextField();
		text_totalprice.setEditable(false);
		text_totalprice.setColumns(10);
		
		JLabel lblNewLabel_7_1 = new JLabel("实收：");
		lblNewLabel_7_1.setFont(new Font("宋体", Font.BOLD, 30));
		
		text_getmoney = new JTextField();
		text_getmoney.setColumns(10);
		Document document=text_getmoney.getDocument();
		document.addDocumentListener(new DocumentAdapter() {
			@Override
			public void insertUpdate(DocumentEvent e) {
				text_change.setText(Math.round(CastUtil.castDouble(text_getmoney.getText())-CastUtil.castDouble(text_totalprice.getText()))+"");
			}
			@Override
			public void removeUpdate(DocumentEvent e) {
				text_change.setText(CastUtil.castDouble(text_getmoney.getText())-CastUtil.castDouble(text_totalprice.getText())+"");
			}
		});
		
		JLabel lblNewLabel_7_2 = new JLabel("原价：");
		lblNewLabel_7_2.setFont(new Font("宋体", Font.BOLD, 30));
		
		text_yuanprice = new JTextField();
		text_yuanprice.setEditable(false);
		text_yuanprice.setColumns(10);
		
		JLabel lblNewLabel_7_2_1 = new JLabel("折扣：");
		lblNewLabel_7_2_1.setFont(new Font("宋体", Font.BOLD, 30));
		
		text_discount = new JTextField();
		text_discount.setEditable(false);
		text_discount.setColumns(10);
		
		JButton btnNewButton_2 = new JButton("结算");
		btnNewButton_2.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				//生成订单并且生成账单
				OrderListService ols=new OrderListServiceImpl();
				boolean flag=false;
				for (OrderFood orderFood : orderlist) {
					OrderList orderList=new OrderList();
					//把orderfood对象转换为orderlist对象
					orderList.setOrderNo(orderFood.getOrderNo());
					orderList.setWid(orderFood.getWid());
					orderList.setTid(orderFood.getTid());
					orderList.setMid(orderFood.getMid());
					orderList.setFoodName(orderFood.getFoodName());
					orderList.setFoodPrice(orderFood.getFoodPrice());
					orderList.setFoodNum(orderFood.getFoodNum());
					orderList.setOrderTime(orderFood.getOrderTime());
					orderList.setRemark(orderFood.getRemark());
					//删除挂单表的数据
					boolean flag2=foodService.deleteByOrderNo(orderFood.getOrderNo());
					if(ols.add(orderList)&&flag2) {
						flag=true;
					}
				}
				boolean flag1=false;
				//生成账单，账单是本次消费总金额
				Bill bill=new Bill();
				bill.setOrderNo(orderlist.get(0).getOrderNo());
				bill.setOriginalPrice(CastUtil.castDouble(text_yuanprice.getText()));
				bill.setDiscountPrice(CastUtil.castDouble(text_totalprice.getText()));
				bill.setGetMoney(CastUtil.castDouble(text_getmoney.getText()));
				bill.setChangeMoney(CastUtil.castDouble(text_change.getText()));
				BillService billService=new BillServiceImpl();
				if(billService.add(bill)) {
					flag1=true;
				}
				
				if(flag&&flag1) {
					JOptionPane.showMessageDialog(V_Pay.this, "结算成功","成功提示",JOptionPane.INFORMATION_MESSAGE);
					//会员积分累加
					vip.setIntegral(Math.round(CastUtil.castInt(text_yuanprice.getText())));
					vip.setCostMoney(vip.getCostMoney()+CastUtil.castDouble(text_yuanprice.getText()));;
					vip.setCostNum(vip.getCostNum()+1);
					vipService.updateVip(vip);
					//修改桌子的状态
					TableService tableService=new TableServiceImpl();
					Table updateTable = tableService.findById(orderlist.get(0).getTid());
					if(updateTable.getStatus()==1) {
						updateTable.setStatus(0);
					}
					tableService.update(updateTable);
					initComBox();
					initTable(foodService.findAll());
					clear();
				}else {
					JOptionPane.showMessageDialog(V_Pay.this, "结算失败","失败提示",JOptionPane.ERROR_MESSAGE);
				}
				
			}
		});
		btnNewButton_2.setFont(new Font("宋体", Font.BOLD, 30));
		
		JLabel lblNewLabel_7_1_1 = new JLabel("找零：");
		lblNewLabel_7_1_1.setFont(new Font("宋体", Font.BOLD, 30));
		
		text_change = new JTextField();
		text_change.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				text_change.setText(CastUtil.castDouble(text_getmoney.getText())-CastUtil.castDouble(text_totalprice)+"");
			}
		});
		text_change.setEditable(false);
		text_change.setColumns(10);
		
		JLabel lblNewLabel_3 = new JLabel("当前会员：");
		
		lbl_vip = new JLabel("未选择会员");
		lbl_vip.setForeground(Color.RED);
		
		JLabel lblNewLabel_3_1 = new JLabel("卡号：");
		
		lbl_orderNo = new JLabel("未显示");
		lbl_orderNo.setForeground(Color.RED);
		
		lbl_integral = new JLabel("0");
		lbl_integral.setForeground(Color.RED);
		
		JLabel lblNewLabel_3_1_1 = new JLabel("积分：");
		
		table = new JTable();
		table.setModel(new DefaultTableModel(
			new Object[][] {
			},
			new String[] {
				"\u8BA2\u5355\u7F16\u53F7", "\u670D\u52A1\u5458", "\u684C\u53F7", "\u83DC\u540D", "\u5355\u4EF7", "\u6570\u91CF", "\u5206\u7C7B", "\u5C0F\u8BA1"
			}
		));
		scrollPane.setViewportView(table);
		GroupLayout gl_contentPane = new GroupLayout(contentPane);
		gl_contentPane.setHorizontalGroup(
			gl_contentPane.createParallelGroup(Alignment.LEADING)
				.addGroup(gl_contentPane.createSequentialGroup()
					.addGap(21)
					.addComponent(lblNewLabel_1, GroupLayout.PREFERRED_SIZE, 71, GroupLayout.PREFERRED_SIZE)
					.addGap(4)
					.addComponent(cb_table, 0, 110, Short.MAX_VALUE)
					.addGap(18)
					.addComponent(btnNewButton, GroupLayout.DEFAULT_SIZE, 57, Short.MAX_VALUE)
					.addGap(244)
					.addComponent(lblNewLabel)
					.addGap(4)
					.addComponent(cb_waiter, 0, 85, Short.MAX_VALUE)
					.addGap(4)
					.addComponent(lblNewLabel_6, GroupLayout.PREFERRED_SIZE, 47, GroupLayout.PREFERRED_SIZE)
					.addGap(4)
					.addComponent(text_search, GroupLayout.DEFAULT_SIZE, 138, Short.MAX_VALUE)
					.addGap(18)
					.addComponent(btnNewButton_1, GroupLayout.DEFAULT_SIZE, 63, Short.MAX_VALUE)
					.addGap(24))
				.addGroup(gl_contentPane.createSequentialGroup()
					.addGap(10)
					.addComponent(scrollPane, GroupLayout.DEFAULT_SIZE, 511, Short.MAX_VALUE)
					.addGap(22)
					.addComponent(lblNewLabel_3)
					.addGap(6)
					.addGroup(gl_contentPane.createParallelGroup(Alignment.LEADING)
						.addGroup(gl_contentPane.createSequentialGroup()
							.addComponent(lbl_vip)
							.addGap(18)
							.addComponent(lblNewLabel_3_1, GroupLayout.PREFERRED_SIZE, 48, GroupLayout.PREFERRED_SIZE)
							.addGap(6)
							.addComponent(lbl_orderNo, GroupLayout.PREFERRED_SIZE, 60, GroupLayout.PREFERRED_SIZE)
							.addGap(29)
							.addComponent(lblNewLabel_3_1_1, GroupLayout.PREFERRED_SIZE, 48, GroupLayout.PREFERRED_SIZE))
						.addGroup(gl_contentPane.createSequentialGroup()
							.addGap(4)
							.addComponent(lblNewLabel_7_2)
							.addGap(4)
							.addComponent(text_yuanprice, GroupLayout.DEFAULT_SIZE, 169, Short.MAX_VALUE))
						.addGroup(gl_contentPane.createSequentialGroup()
							.addGap(4)
							.addComponent(lblNewLabel_7_2_1)
							.addGap(4)
							.addComponent(text_discount, GroupLayout.DEFAULT_SIZE, 169, Short.MAX_VALUE))
						.addGroup(gl_contentPane.createSequentialGroup()
							.addGap(4)
							.addComponent(lblNewLabel_7)
							.addGap(4)
							.addComponent(text_totalprice, GroupLayout.DEFAULT_SIZE, 169, Short.MAX_VALUE))
						.addGroup(gl_contentPane.createSequentialGroup()
							.addGap(4)
							.addComponent(lblNewLabel_7_1)
							.addGap(4)
							.addComponent(text_getmoney, GroupLayout.DEFAULT_SIZE, 169, Short.MAX_VALUE))
						.addGroup(gl_contentPane.createSequentialGroup()
							.addGap(4)
							.addComponent(lblNewLabel_7_1_1)
							.addGap(4)
							.addComponent(text_change, GroupLayout.DEFAULT_SIZE, 169, Short.MAX_VALUE))
						.addGroup(gl_contentPane.createSequentialGroup()
							.addGap(120)
							.addComponent(btnNewButton_2, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
							.addGap(56)))
					.addGap(6)
					.addComponent(lbl_integral, GroupLayout.PREFERRED_SIZE, 60, GroupLayout.PREFERRED_SIZE)
					.addGap(29))
				.addGroup(gl_contentPane.createSequentialGroup()
					.addGap(20)
					.addComponent(lblNewLabel_2, GroupLayout.PREFERRED_SIZE, 46, GroupLayout.PREFERRED_SIZE)
					.addGap(6)
					.addComponent(lbl_num, GroupLayout.PREFERRED_SIZE, 53, GroupLayout.PREFERRED_SIZE)
					.addGap(228)
					.addComponent(lblNewLabel_4)
					.addGap(6)
					.addComponent(lbl_sum, GroupLayout.PREFERRED_SIZE, 83, GroupLayout.PREFERRED_SIZE))
		);
		gl_contentPane.setVerticalGroup(
			gl_contentPane.createParallelGroup(Alignment.LEADING)
				.addGroup(gl_contentPane.createSequentialGroup()
					.addGap(30)
					.addGroup(gl_contentPane.createParallelGroup(Alignment.LEADING)
						.addGroup(gl_contentPane.createSequentialGroup()
							.addGap(2)
							.addComponent(lblNewLabel_1))
						.addGroup(gl_contentPane.createSequentialGroup()
							.addComponent(cb_table)
							.addGap(3))
						.addGroup(gl_contentPane.createSequentialGroup()
							.addComponent(btnNewButton, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
							.addGap(3))
						.addGroup(gl_contentPane.createSequentialGroup()
							.addGap(5)
							.addComponent(lblNewLabel))
						.addGroup(gl_contentPane.createSequentialGroup()
							.addGap(3)
							.addComponent(cb_waiter, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
							.addGap(0, 0, Short.MAX_VALUE))
						.addGroup(gl_contentPane.createSequentialGroup()
							.addGap(5)
							.addComponent(lblNewLabel_6))
						.addGroup(gl_contentPane.createSequentialGroup()
							.addGap(4)
							.addComponent(text_search, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
							.addPreferredGap(ComponentPlacement.RELATED, 1, Short.MAX_VALUE))
						.addGroup(gl_contentPane.createSequentialGroup()
							.addGap(3)
							.addComponent(btnNewButton_1)
							.addGap(0, 0, Short.MAX_VALUE)))
					.addGap(3)
					.addGroup(gl_contentPane.createParallelGroup(Alignment.LEADING)
						.addComponent(scrollPane, GroupLayout.DEFAULT_SIZE, 438, Short.MAX_VALUE)
						.addGroup(gl_contentPane.createSequentialGroup()
							.addGap(27)
							.addComponent(lblNewLabel_3))
						.addGroup(gl_contentPane.createSequentialGroup()
							.addGap(27)
							.addGroup(gl_contentPane.createParallelGroup(Alignment.LEADING)
								.addComponent(lbl_vip)
								.addComponent(lblNewLabel_3_1)
								.addComponent(lbl_orderNo)
								.addComponent(lblNewLabel_3_1_1))
							.addGap(6)
							.addGroup(gl_contentPane.createParallelGroup(Alignment.LEADING)
								.addComponent(lblNewLabel_7_2)
								.addComponent(text_yuanprice, GroupLayout.PREFERRED_SIZE, 35, GroupLayout.PREFERRED_SIZE))
							.addGap(28)
							.addGroup(gl_contentPane.createParallelGroup(Alignment.LEADING)
								.addComponent(lblNewLabel_7_2_1)
								.addComponent(text_discount, GroupLayout.PREFERRED_SIZE, 35, GroupLayout.PREFERRED_SIZE))
							.addGap(24)
							.addGroup(gl_contentPane.createParallelGroup(Alignment.LEADING)
								.addComponent(lblNewLabel_7)
								.addComponent(text_totalprice, GroupLayout.PREFERRED_SIZE, 35, GroupLayout.PREFERRED_SIZE))
							.addGap(27)
							.addGroup(gl_contentPane.createParallelGroup(Alignment.LEADING)
								.addComponent(lblNewLabel_7_1)
								.addComponent(text_getmoney, GroupLayout.PREFERRED_SIZE, 35, GroupLayout.PREFERRED_SIZE))
							.addGap(18)
							.addGroup(gl_contentPane.createParallelGroup(Alignment.LEADING)
								.addComponent(lblNewLabel_7_1_1)
								.addComponent(text_change, GroupLayout.PREFERRED_SIZE, 35, GroupLayout.PREFERRED_SIZE))
							.addGap(41)
							.addComponent(btnNewButton_2))
						.addGroup(gl_contentPane.createSequentialGroup()
							.addGap(27)
							.addComponent(lbl_integral)))
					.addGap(6)
					.addGroup(gl_contentPane.createParallelGroup(Alignment.LEADING)
						.addComponent(lblNewLabel_2)
						.addComponent(lbl_num)
						.addGroup(gl_contentPane.createSequentialGroup()
							.addGap(2)
							.addComponent(lblNewLabel_4))
						.addComponent(lbl_sum))
					.addGap(43))
		);
		contentPane.setLayout(gl_contentPane);
		
		//初始化订单下拉框
		initComBox();
//		initTable(ofs.findByOrderNo(String.valueOf(cb_table.getSelectedItem())));
		//初始化服务员下拉框
		initWaiter();
		//初始化表格
		initTable(foodService.findAll());
	}
	//初始化餐桌下拉框
	public void initComBox() {
		DefaultComboBoxModel<String> model=new DefaultComboBoxModel<>();
		model.addElement("---请选择---");
		OrderFoodService ofs=new OrderFoodServiceImpl();
		Map<String, Integer> ordermap = ofs.getOrderCount();
		Set<String> key = ordermap.keySet();
		for (String k:key) {
			model.addElement(k);
		}
		cb_table.setModel(model);
	}
	//初始化收银员下拉框
	public void initWaiter() {
		DefaultComboBoxModel<Waiter> model=new DefaultComboBoxModel<>();
		WaiterService service=new WaiterServiceImpl();
		List<Waiter> waiterList = service.findAllWaiter();
		System.out.println(waiterList);
		for (Waiter waiter : waiterList) {
			model.addElement(waiter);
		}
		cb_waiter.setModel(model);
	}
	
	/**
	 * 初始化表格
	 */
	public void initTable(List<OrderFood> list) {
		//清空表格
		DefaultTableModel model = (DefaultTableModel) table.getModel();
		int rowCount = model.getRowCount();
		if(rowCount!=0) {
			for (int i = 0; i < rowCount; i++) {
				model.removeRow(0);
			}
		}
		//初始化
		for (OrderFood orderFood : list) {
			model.addRow(new Object[] {
					orderFood.getOrderNo(),
					orderFood.getWid(),
					orderFood.getTid(),
					orderFood.getFoodName(),
					orderFood.getFoodPrice(),
					orderFood.getFoodNum(),
					orderFood.getMid(),
					orderFood.getFoodPrice()*orderFood.getFoodNum()
			});
		}
	}
	/**
	 * 结算成功，清空数据
	 */
	public void clear() {
		lbl_integral.setText("0");
		lbl_num.setText("0");
		lbl_orderNo.setText("未选择会员");
		lbl_sum.setText("0");
		lbl_vip.setText("未选择会员");
		text_change.setText("");
		text_discount.setText("");
		text_getmoney.setText("");
		text_totalprice.setText("");
		text_yuanprice.setText("");
	}
}
